
#include "winsockinitializer.h"
#include "win32.h"

namespace tinkle {

#ifdef WIN32

// Use a global initializer, so WSAStartup happens at module load time.
// I learned this method from libjingle' s source code.

class WinsockInitializer {
public:
    WinsockInitializer() {
        WSADATA wsaData;
        WORD wVersionRequired = MAKEWORD(2, 2);
        _err = WSAStartup(wVersionRequired, &wsaData);
    }
    ~WinsockInitializer() {
        if (!_err) {
            WSACleanup();
        }
    }
    int WinsockInitializer::error() {
        return _err;
    }

private:
    int _err;
};

WinsockInitializer g_winsockinit;

#endif

}   //namespace tinkle